package com.wzgl.core.baseModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wzgl.core.utils.UuidUtil;

/***
 * Db Record 操作工具类
 * @author Administrator
 *
 */
public class RecordBaseDao {
	private Log log=Log.getLog(RecordBaseDao.class);
	/***
	 * 保存到数据库
	 * @param map	保存内容
	 * @param tableName	表名称
	 * @return
	 * @throws Exception
	 */
	public static boolean saveObject(Map<String, String[]> map,String tableName) throws Exception{
		Record r = mapToRecord(map);
		if(r.getStr("fid")==null || "".equals(r.getStr("fid"))){
			r.set("fid", UuidUtil.get32UUID());
		}
		boolean b = false;
		b = Db.save(tableName, r);
		return b;
	}
	public static boolean insertObject(Map<String, String> map,String tableName) throws Exception{
		Record r = toRecord(map);
		if(r.getStr("fid")==null || "".equals(r.getStr("fid"))){
			r.set("fid", UuidUtil.get32UUID());
		}
		boolean b = false;
		b = Db.save(tableName, r);
		return b;
	}
	/***
	 * 更新到数据库
	 * @param map
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public static boolean updateObject(Map<String, String[]> map,String tableName) throws Exception{
		Record r = mapToRecord(map);	
		boolean b = false;
		b = Db.update(tableName,"fid", r);
		return b;
	}
	public static boolean editObject(Map<String, String> map,String tableName) throws Exception{
		Record r = toRecord(map);	
		boolean b = false;
		b = Db.update(tableName,"fid", r);
		return b;
	}
	/***
	 * 保存或者更新
	 * @param map
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public static boolean saveOrupdate(Map<String, String[]> map,String tableName) throws Exception{
		Record r = mapToRecord(map);	
		boolean b = false;
		String fid = r.getStr("fid");
		if(fid==null || "".equals(fid)){
			r.set("fid", UuidUtil.get32UUID());
			b = Db.save(tableName, r);
		}else{
			b = Db.update(tableName,"fid", r);
		}
		return b;
	}
	/***
	 * 保存或者更新
	 * @param map
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public static boolean insertOrEdit(Map<String, String> map,String tableName) throws Exception{
		Record r = toRecord(map);	
		boolean b = false;
		String fid = r.getStr("fid");
		if(fid==null || "".equals(fid)){
			r.set("fid", UuidUtil.get32UUID());
			b = Db.save(tableName, r);
		}else{
			b = Db.update(tableName,"fid", r);
		}
		return b;
	}
	/**
	 * 根据ID查询数据库
	 * @param fid
	 * @param tableName
	 * @return
	 */
	public static Record findById(String fid,String tableName)throws Exception{
		return Db.findById(tableName, fid);
	}
	/***
	 * 根据ID删除数据库
	 * @param fid
	 * @param tableName
	 * @return
	 */
	public static boolean deleteById(String fid,String tableName)throws Exception{
		return Db.deleteById(tableName, fid);
	}
	/**
	 * 根据条查询数据
	 * @param map	条件
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public static List<Record> find(Map<String, String> map,String tableName)throws Exception{
		List<Record> list = new ArrayList<Record>();
		String sql = "select * from " + tableName;
		if(map != null){
			sql+=getParams(map);
		}
		list= Db.find(sql);
		return list;
	}
	/**
	 * 根据条查询数据
	 * @param map	条件
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public static List<Record> find(Map<String, String> map,String tableName,String order,int count)throws Exception{
		List<Record> list = new ArrayList<Record>();
		String sql = "select * from " + tableName;
		if(map != null){
			sql+=getParams(map);
		}
		if(!"".equals(order) && order != null){
			sql+= " order by" + order;
		}
		if(count > 0){
			sql+= " LIMIT " + count;
		}
		list= Db.find(sql);
		return list;
	}
	/**
	 * 查询语句条件封装
	 * @param map
	 * @return
	 */
	private static String getParams(Map<String, String> map){
		StringBuffer sb = new StringBuffer();
		sb.append(" where 1 = 1 ");
		for (Map.Entry<String, String> entry : map.entrySet()) { 
			String w = " and "+ entry.getKey() + "=" +entry.getValue();
			sb.append(w);
		}
		return sb.toString();
	}
	/***
	 * Map<String, String[]> 转 mapMap<String, String>
	 * @param map
	 * @return
	 */
	private static Record mapToRecord(Map<String, String[]> map){
		Record r = new Record();
		for (Map.Entry<String, String[]> entry : map.entrySet()) { 
			String key = entry.getKey();
			Object value = entry.getValue();
			String val = null;
			if (value != null && ((String[])value)[0] !=null && !"".equals(((String[])value)[0])) {
				val = ((String[])value)[0];
				r.set(key, val);  
			}
		}  
		return r;
	}
	/***
	 * Map<String, String[]> 转 mapMap<String, String>
	 * @param map
	 * @return
	 */
	private static Record toRecord(Map<String, String> map){
		Record r = new Record();
		for (Map.Entry<String, String> entry : map.entrySet()) { 
			String key = entry.getKey();
			Object value = entry.getValue();
			if (value != null && !"".equals(value)) {
				r.set(key, value);  
			}
		}  
		return r;
	}
}
